let dao = require('../database/dao'),
  logger = require('../helper/logger'),
  tagLogger = new logger.TagLogger('REPORT_MODEL'),
  errorCode = require('../../conf/errorCode'),
  commonModel = require('./commonModel'),
  util = require('../helper/util'),
  _ = require('lodash');

let {
  genUpdateCallback,
  genInsertCallback,
  genUpdateRslt,
  genUpdateBatchRslt,
  genUpdateBatchCallback,
  genSelectCallback,
  genPagination,
  genInertRslt,
} = commonModel;

//获取举报列表
exports.getReportByPage = function(page, size, callback) {
  let [offset, limit] = util.ps2ol(page, size),
    fromJoinWhere = `FROM report as r 
                        LEFT JOIN user as u
              		    ON r.uid = u.id 
                        LEFT JOIN post as p
              		    ON r.postId = p.id 
                                where u.status = 0 and p.status>=0 and r.status>=0`;
  let postQuery = {
    str: `SELECT r.id reportId,r.content reason,r.ctime reportTime,p.id articleId,p.title articleTitle,p.channel channel,u.accountName authorName ${fromJoinWhere} order by r.ctime desc LIMIT ?,? `,
    param: [offset, limit],
  };
  let countQuery = {
    str: `SELECT count(1) as count ${fromJoinWhere}`,
    param: [],
  };
  let queryArr = [postQuery, countQuery];
  dao.queryBatch(queryArr, (err, rslts) => {
    if (err == null)
      callback(null, genPagination(rslts[0], page, size, rslts[1][0].count));
    else callback(err, null);
  });
};

//删除举报-支持批量删除
exports.deleteReportByIds = function(ids, callback) {
  dao.deleteBatch('report', ids, callback);
};
